//
//  DynamicColumnFieldViewController.m
//  mcrmSAAS
//
//  Created by wjxfhxy on 15-3-9.
//  Copyright (c) 2015年 GuangZhouXuanWu. All rights reserved.
//

#import "DynamicColumnFieldViewController.h"

@interface DynamicColumnFieldViewController ()
{
}
@end

@implementation DynamicColumnFieldViewController

- (void)initViewDidLoad
{
    [self setBackNavigateItemTitle:@"" rightItemText:_vclType == DynamicColumnFieldViewTypeReview ? @"编辑" :@"提交"];
    
    if(_vclType == DynamicColumnFieldViewTypeAdd || _vclType == DynamicColumnFieldViewTypeModify)
        [self addKeyboardObserver];
    
    _manager = [[RSWDataOperation alloc] initWithDataOperationManagerDelegate:self];
    
    _core = [DynamicColumnFieldCore new];
    _core.delegate = self;
    _core.dataSource = self;
    [_core loadView];
    
    if (@available(iOS 11.0, *))
    {
        XUIGroupTableView *groudView = [_core getGrouptableView];
        XUITableView *_tableView = [groudView getTableView];
        
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        
        CGFloat top = _tableView.contentInset.top+[XUINavigationController navigationController].navigationBar.bottom;
        CGFloat left = _tableView.contentInset.left;
        CGFloat bottom = _tableView.contentInset.bottom;
        CGFloat right = _tableView.contentInset.right;
        _tableView.contentInset = UIEdgeInsetsMake(top,left,bottom,right);
        _tableView.scrollIndicatorInsets = UIEdgeInsetsMake(top,left,bottom,right);
    }
}

- (XUIGroupTableView*)getGrouptableView
{
    return  [_core getGrouptableView];
}

-(void)viewControllerDidMoveAppear
{
    [super viewControllerDidMoveAppear];
    
    if(_vclType == DynamicColumnFieldViewTypeAdd || _vclType == DynamicColumnFieldViewTypeModify)
        [XUINavigationController enableSwipeToLeftViewController:NO];
}

-(void)viewControllerDidMoveReappear
{
    [super viewControllerDidMoveReappear];
    
    if(_vclType == DynamicColumnFieldViewTypeAdd || _vclType == DynamicColumnFieldViewTypeModify)
        [XUINavigationController enableSwipeToLeftViewController:NO];
}

-(void)viewControllerDidMoveDealloc
{
    [super viewControllerDidMoveDealloc];
    
    if(_vclType == DynamicColumnFieldViewTypeAdd || _vclType == DynamicColumnFieldViewTypeModify)
        [XUINavigationController enableSwipeToLeftViewController:YES];
}

-(void)viewControllerWillDealloc
{
    [super viewControllerWillDealloc];
    
    [_manager terminateRequest];
    
    [_core unloadView];
    
    [self removeObserver];
}

-(void)clickedBackBarButton:(id)sender
{
    [XUINavigationController pop];
}

#pragma mark --
#pragma mark -- progressView

- (void)beginProgressView:(XMSDataOperationManager *)dom
{
    [self beginProgressViewPanel:@"提交中，请稍候"];
}

- (void)beginProgressView:(XMSDataOperationManager *)dom funcationName:(NSString *)szFuncationname
{
    [self beginProgressViewPanel:@"提交中，请稍候"];
}

- (void)showProgressView:(NSString*)title
{
    [self beginProgressViewPanel:title];
}

- (void)hideProgressView
{
    [self endProgressViewPanel];
}

#pragma mark --
#pragma mark -- keyBoard

- (BOOL)keyboardDidShow:(NSNotification *)noti
{
    BOOL bResult = [super keyboardDidShow:noti];
    if (YES == bResult)
    {
        [_core keyBoardDidShow:self.keyboardOffSet];
    }
    return bResult;
}

-(BOOL)keyboardWillHide:(NSNotification *)noti
{
    BOOL bResult = [super keyboardWillHide:noti];
    if(YES == bResult)
    {
        [_core keyBoardWillHide:self.keyboardOffSet];
    }
    return bResult;
}

- (void)keyboardWillChangeSize:(CGFloat)changeSize withAnimationDuration:(CGFloat)duration curve:(UIViewAnimationCurve)curve
{
    [_core keyBoardFrameWillChange:changeSize];
}

#pragma mark --
#pragma mark dynamicColumnFieldCore dataSource --

-(UIView*)superViewOfDynamicColumnFieldCore:(DynamicColumnFieldCore *)dynamicColumnFieldCore
{
    return self.view;
}

-(CGRect)frameOfDynamicColumnFieldCore:(DynamicColumnFieldCore *)dynamicColumnFieldCore
{
    return self.view.bounds;
}

-(id)packObjOfDynamicColumnFieldCore:(DynamicColumnFieldCore *)dynamicColumnFieldCore
{
    return _packObj;
}

-(XMSQueueDictionary*)constraintOfDynamicColumnFieldCore:(DynamicColumnFieldCore*)dynamicColumnFieldCore
{
    return _constraint;
}

-(XMSQueueDictionary*)transformMapOfDynamicColumnFieldCore:(DynamicColumnFieldCore *)dynamicColumnFieldCore
{
    return _transformMap;
}

-(DynamicColumnFieldViewType)typeOfDynamicColumnFieldCore:(DynamicColumnFieldCore *)dynamicColumnFieldCore
{
    return _vclType;
}

-(id)dynamicColumnFieldCore:(DynamicColumnFieldCore*)dynamicColumnFieldCore formatDynamicColumnCellValue:(id)value fieldDescript:(FieldDescript*)fieldDescript
{
    return [self formatDynamicColumnCellValue:value fieldDescript:fieldDescript];
}

#pragma mark --
#pragma mark dynamicColumnFieldCore delegate --

-(CGFloat)dynamicColumnFieldCore:(DynamicColumnFieldCore*)dynamicColumnFieldCore dynamicColumnCellWillCaleHeight:(FieldDescript*)fieldDescript
{
    return [self dynamicColumnCellWillCaleHeight:fieldDescript];
}

-(ExpandDynamicCell*)dynamicColumnFieldCore:(DynamicColumnFieldCore*)dynamicColumnFieldCore dynamicColumnCellWillCreate:(FieldDescript*)fieldDescript
{
    return [self dynamicColumnCellWillCreate:fieldDescript];
}

-(void)dynamicColumnFieldCore:(DynamicColumnFieldCore*)dynamicColumnFieldCore dynamicColumnCell:(ExpandDynamicCell *)cell didCreate:(FieldDescript *)fieldDescript
{
    [self dynamicColumnCell:cell didCreate:fieldDescript];
}

-(BOOL)dynamicColumnFieldCore:(DynamicColumnFieldCore*)dynamicColumnFieldCore handlerDynamicColumnCellDidSelected:(FieldDescript*)fieldDescript
{
    return [self handlerDynamicColumnCellDidSelected:fieldDescript];
}

-(NSInteger)dynamicColumnFieldCore:(DynamicColumnFieldCore*)dynamicColumnFieldCore pickerDefaultRowInFieldDescript:(FieldDescript*)fieldDescript
{
    return [self pickerDefaultRowInFieldDescript:fieldDescript];
}

-(NSInteger)dynamicColumnFieldCore:(DynamicColumnFieldCore*)dynamicColumnFieldCore pickerNumberOfRowInFieldDescript:(FieldDescript*)fieldDescript
{
    return [self pickerNumberOfRowInFieldDescript:fieldDescript];
}

- (NSString*)dynamicColumnFieldCore:(DynamicColumnFieldCore*)dynamicColumnFieldCore pickerTitleForRow:(NSInteger)row inFieldDescript:(FieldDescript*)fieldDescript
{
    return [self pickerTitleForRow:row inFieldDescript:fieldDescript];
}

- (NSString*)dynamicColumnFieldCore:(DynamicColumnFieldCore*)dynamicColumnFieldCore pickerSelectedTitleAtIndex:(NSInteger)nIndex inFieldDescript:(FieldDescript*)fieldDescript
{
    return [self pickerSelectedTitleAtIndex:nIndex inFieldDescript:fieldDescript];
}

- (NSDate*)dynamicColumnFieldCore:(DynamicColumnFieldCore*)dynamicColumnFieldCore datePickerDefaultDate:(NSString *)dateString fieldDescript:(FieldDescript *)fieldDescript
{
    return [self datePickerDefaultDate:dateString fieldDescript:fieldDescript];
}

- (void)dynamicColumnFieldCore:(DynamicColumnFieldCore*)dynamicColumnFieldCore didSelectedSwitchSelectGroupTableCell:(FieldDescript*)fieldDescript
{
    [self didSelectedSwitchSelectGroupTableCell:fieldDescript];
}

- (id)dynamicColumnFieldCore:(DynamicColumnFieldCore *)dynamicColumnFieldCore groupTitleInGroup:(NSUInteger)group
{
    return [self groupTitleInGroup:group];
}

#pragma mark --
#pragma mark -- recv lastVcl paramkey

- (void)recvPopViewControllerSwitchParam:(XUIViewControllerSwitchParam *)switchParam
{
    [_core recvPopViewControllerSwitchParam:switchParam];
}

#pragma mark --
#pragma mark -- override

- (CGFloat)dynamicColumnCellWillCaleHeight:(FieldDescript*)fieldDescript
{
    return 0;
}

- (ExpandDynamicCell*)dynamicColumnCellWillCreate:(FieldDescript*)fieldDescript
{
    return nil;
}

- (void)dynamicColumnCell:(ExpandDynamicCell *)cell didCreate:(FieldDescript *)fieldDescript
{
    
}

- (id)formatDynamicColumnCellValue:(id)value fieldDescript:(FieldDescript*)fieldDescript
{
    return value;
}

- (BOOL)handlerDynamicColumnCellDidSelected:(FieldDescript*)fieldDescript
{
    return NO;
}

- (NSDate*)datePickerDefaultDate:(NSString *)dateString fieldDescript:(FieldDescript *)fieldDescript
{
    return nil;
}

- (NSInteger)pickerDefaultRowInFieldDescript:(FieldDescript*)fieldDescript
{
    return -1;
}

- (NSInteger)pickerNumberOfRowInFieldDescript:(FieldDescript*)fieldDescript
{
    return -1;
}

- (NSString*)pickerTitleForRow:(NSInteger)row inFieldDescript:(FieldDescript*)fieldDescript
{
    return nil;
}

- (NSString*)pickerSelectedTitleAtIndex:(NSInteger)nIndex inFieldDescript:(FieldDescript*)fieldDescript
{
    return nil;
}

- (void)didSelectedSwitchSelectGroupTableCell:(FieldDescript*)fieldDescript
{
    
}

- (id)groupTitleInGroup:(NSUInteger)group
{
    return nil;
}

#pragma mark --
#pragma mark -- reload and verifty

-(void)reloadDyanmicField
{
    [_core reloadDyanmicField];
}

- (BOOL)verifyDynamicField:(XMSQueueDictionary*)constraint
{
    return [_core verifyDynamicField:constraint];
}

- (void)extractDynamicFieldValue:(XMSQueueDictionary*)constraint transformMap:(XMSQueueDictionary*)transformMap packObj:(id)packObj specialExtract:(BOOL(^)(FieldDescript *fieldDescript))specialExtract
{
    [_core extractDynamicFieldValue:constraint transformMap:transformMap packObj:packObj specialExtract:specialExtract];
}

@end
